package com.wisdytech.linkdcs.equipLifeCycle.malfunction.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dao.ITPReplaceTempDao;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.TPReplaceTemp;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.service.ITPReplaceTempService;
import com.wisdytech.linkdcs.tool.service.IEqToolConfigService;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dailibo
 * @since 2018-06-12
 */
@Service("iTPReplaceTempService")
@Transactional
public class TPReplaceTempServiceImpl extends BaseService<ITPReplaceTempDao, TPReplaceTemp> implements ITPReplaceTempService {

	/**
	 * 
	 */
	@Autowired
	private ITPReplaceTempDao tempDao;
	@Autowired
	private IEqToolConfigService iEqToolConfigService;
	
	@Override
	public boolean add(TPReplaceTemp temp) {
		EntityWrapper<TPReplaceTemp> ew = new EntityWrapper<>();
		ew.eq("N_SERIAL_NUM", temp.getnSerialNum());
		ew.eq("flag", temp.getFlag());
		int count = tempDao.selectCount(ew);
		if(count>0) {
			throw new ServiceException("新序列号已被添加");
		}
		return temp.insert();
	}

	@Override
	public boolean del(String pkId) {
		return  SqlHelper.retBool(tempDao.deleteById(pkId));
	}

	@Override
	public boolean update(TPReplaceTemp temp) {
		EntityWrapper<TPReplaceTemp> ew = new EntityWrapper<>();
		ew.eq("N_SERIAL_NUM", temp.getnSerialNum());
		ew.eq("flag", temp.getFlag());
		int count = tempDao.selectCount(ew);
//		if(count>0) {
//			throw new ServiceException("新序列号已被添加");
//		}
		return temp.updateById();
	}

	@Override
	public PageInfo<TPReplaceTemp> getTemps(Filter filter, String planId) {
		EntityWrapper<TPReplaceTemp> ew =new EntityWrapper<>();
		ew.eq("PLAN_ID", planId);
		ew.eq("FLAG", "0");
		List<TPReplaceTemp>  result =
				this.beforeList(ITPReplaceTempDao.class, filter).selectList(ew);
		return new PageInfo<>(result);
	}

	@Override
	public TPReplaceTemp getTempById(String pkId) {
		return tempDao.selectById(pkId);
	}

	@Override
	public TPReplaceTemp getTempAllById(String pkId) {
		TPReplaceTemp tPReplaceTemp = tempDao.getTempAllById(pkId);
		return tPReplaceTemp;
	}

	@Override
	public PageInfo<TPReplaceTemp> getRecordTemps(Filter filter, String pkId) {
		List<TPReplaceTemp>  result =
				this.beforeList(ITPReplaceTempDao.class, filter).getRecordTemps(pkId);
		return new PageInfo<>(result);
	}

	@Override
	public void addTool(TPReplaceTemp tPReplaceTemp) {
        //校验工装状态
        if (!iEqToolConfigService.checkToolStatus(tPReplaceTemp.getCode()))
            throw new ServiceException("工装状态不可用");
        this.add(tPReplaceTemp);
	}

	@Override
	public PageInfo<TPReplaceTemp> getRecordTempsByMal(Filter filter, String pkId) {
		List<TPReplaceTemp>  result =
				this.beforeList(ITPReplaceTempDao.class, filter).getRecordTempsByMal(pkId);
		return new PageInfo<>(result);
	}

    @Override
    public boolean updateTPReplaceTemp(TPReplaceTemp tPReplaceTemp) {
        EntityWrapper<TPReplaceTemp> ew = new EntityWrapper<>();
        ew.eq("N_SERIAL_NUM", tPReplaceTemp.getnSerialNum());
        ew.eq("flag", tPReplaceTemp.getFlag());
        int count = tempDao.selectCount(ew);
        return tPReplaceTemp.updateById();
    }

}
